package cn.edu.uestc.smgt.controller.yh;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.uestc.smgt.pojo.User;
import cn.edu.uestc.smgt.service.AssistStudentService;
import cn.edu.uestc.smgt.util.ViewExcel;

@Controller
@RequestMapping("/yh/ZG/")
public class ZhuGuanController {
	@Autowired
	private AssistStudentService assistStudentService;

	/**
	 * 列出本单位所有助管信息
	 */
	@RequestMapping("listDW")
	@ResponseBody
	public Object listBySelections(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		User yh = (User) session.getAttribute("user");
		if (yh != null) {
			String unitId = yh.getUnitId();
			HashMap map = new HashMap();
			map.put("unitId", unitId);
			return assistStudentService.getAssStuByDW(map);
		} else {
			return null;
		}
	}

	/**
	 * 根据用户选择的列生成excel并让用户下载
	 * 
	 * @return
	 */
	@RequestMapping("downloadZG")
	public ResponseEntity<byte[]> downloadZG(HttpServletRequest request, HttpServletResponse response,
			HttpSession session, @RequestParam String lie) throws IOException {
		System.out.println(lie);
		byte[] body = null;
		InputStream is = request.getSession().getServletContext().getResourceAsStream("/download/ajax.txt");
		body = new byte[is.available()];
		is.read(body);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Disposition", "attchement;filename=test.txt");
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
		return entity;
	}

	/**
	 * 根据用户选择的列生成excel并让用户下载
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("downloadZG1")
	public ModelAndView downloadZG1(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@RequestParam String ny) throws IOException, ParseException {
		User yh = (User) session.getAttribute("user");
		if (yh != null) {
			String unitId = yh.getUnitId();
			HashMap map = new HashMap();
			map.put("unitId", unitId);
			// 获取所有的数据
			List list = assistStudentService.getAssStuByDW(map);
			System.out.println(ny);
			//
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			Date d = format.parse(ny);
			format.applyPattern("yyyyMM");
			String date = format.format(d);
			Map model = new HashMap();
			model.put("date", date);
			model.put("list", assistStudentService.getAllAssStu(null));
			return new ModelAndView(new ViewExcel(), model);
		} else {
			return null;
		}

	}

	/**
	 * 根据选择的列封装数据
	 * 
	 * @param list
	 * @param head
	 * @return
	 */
	private Map getExcelData(List list, String head) {

		return null;
	}
}
